package com.xiguaji.tiktok.v1.tiktok.blogger.service;


import com.xiguaji.boot.toolkit.JsonUtil;
import com.xiguaji.tiktok.utils.RedisKeysManager;
import com.xiguaji.tiktok.v1.common.model.DicLanguage;
import com.xiguaji.tiktok.v1.common.model.DicRegion;
import com.xiguaji.tiktok.v1.common.repository.DicLanguageRepository;
import com.xiguaji.tiktok.v1.common.repository.DicRegionRepository;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Slf4j
@Service
public class DictionaryService {

    /**
     * 语言dao
     */
    @Autowired
    DicLanguageRepository dicLanguageRepository;

    /**
     * 地区dao
     */
    @Autowired
    DicRegionRepository dicRegionRepository;

    /**
     * redis服务
     */
    @Autowired
    RedisTemplate<String,String> redisTemplate;

    /**
     * redis字典过期时间
     */
    @Value("${tiktok.dictionary.expired.day}")
    private int DICTIONARY_EXPIRED;

    /**
     * 获取语言字典
     * @return
     */
    public Map<String,String> getDicLanguageMap(){
        String json = redisTemplate.opsForValue().get(RedisKeysManager.DIC_LANGUAGE);
        Map<String,String> dicMap;
        if(StringUtils.isEmpty(json)){
            val dicLanguages = dicLanguageRepository.findAll();
            Iterator<DicLanguage> dicLanguageList = dicLanguages.iterator();
            dicMap = new HashMap<>();
            DicLanguage dicLanguage;
            while (dicLanguageList.hasNext()){
                dicLanguage = dicLanguageList.next();
                if(dicLanguage != null && StringUtils.isNotEmpty(dicLanguage.getCode())){
                    dicMap.put(dicLanguage.getCode().toLowerCase(),dicLanguage.getName());
                }
            }
            redisTemplate.opsForValue().set(RedisKeysManager.DIC_LANGUAGE,
                    JsonUtil.parseJson(dicMap),DICTIONARY_EXPIRED, TimeUnit.DAYS);
        }else{
            dicMap = JsonUtil.jsonParse(Map.class,json);
        }
        return dicMap;
    }

    /**
     * 获取地区字典
     * @return
     */
    public Map<String,String> getDicRegionMap(){
        String json = redisTemplate.opsForValue().get(RedisKeysManager.DIC_REGION);
        Map<String,String> dicMap;
        if(StringUtils.isEmpty(json)){
            val dicLanguages = dicRegionRepository.findAll();
            Iterator<DicRegion> dicLanguageList = dicLanguages.iterator();
            dicMap = new HashMap<>();
            DicRegion region;
            while (dicLanguageList.hasNext()){
                region = dicLanguageList.next();
                if(region != null && StringUtils.isNotEmpty(region.getCode())){
                    dicMap.put(region.getCode().toLowerCase(),region.getName());
                }
            }
            redisTemplate.opsForValue().set(RedisKeysManager.DIC_REGION,
                    JsonUtil.parseJson(dicMap),DICTIONARY_EXPIRED, TimeUnit.DAYS);
        }else{
            dicMap = JsonUtil.jsonParse(Map.class,json);
        }
        return dicMap;
    }
}
